package com.weixin.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.weixin.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/1418:07
 */
@Component
@Order()
public class RunnerLoad implements CommandLineRunner {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Lazy
    @Autowired
    private RedisUtil redisUtil;

    @Lazy
    @Autowired
    private RestTemplate restTemplate;

    @Lazy
    @Resource
    private ObjectMapper objectMapper;

    @Override
    public void run(String... args) throws Exception {
        log.info("-------开始加载数据------");
       /* String access_token_addressBook =(String) redisUtil.get("access_token_addressBook");
        String access_token_application =(String) redisUtil.get("access_token_application");
        String access_token_customer =(String) redisUtil.get("access_token_customer");
        if (!StringUtils.isEmpty(access_token_addressBook)){
            redisUtil.set("access_token_addressBook","",60*60*2);
        }
        if (!StringUtils.isEmpty(access_token_application)){

        }
        if (!StringUtils.isEmpty(access_token_customer)){

        }
        getAccessToken(WeiXinProperties.getAddressBookSecret());
        getAccessToken(WeiXinProperties.getCorpSecret());
        getAccessToken(WeiXinProperties.getCustomerSecret());*/
        redisUtil.set("access_token_addressBook",getAccessToken(WeiXinProperties.getAddressBookSecret()),60*60*2);
        redisUtil.set("access_token_application",getAccessToken(WeiXinProperties.getCorpSecret()),60*60*2);
        redisUtil.set("access_token_customer",getAccessToken(WeiXinProperties.getCustomerSecret()),60*60*2);
        redisUtil.set("access_token_report",getAccessToken(WeiXinProperties.getReportSecret()),60*60*2);
        redisUtil.set("access_token_conference_room",getAccessToken(WeiXinProperties.getConferenceRoomSecret()),60*60*2);
        redisUtil.set("access_token_conference_network_disk",getAccessToken(WeiXinProperties.getNetworkDiskSecret()),60*60*2);
    }

    private String getAccessToken(String secret) throws IOException {
       // String secret= type != 0 ?WeiXinProperties.getAddressBookSecret() : WeiXinProperties.getCorpSecret();
        StringBuilder urlBuilder=new StringBuilder(WeiXinProperties.getUrl());
        urlBuilder.append("cgi-bin/gettoken?");
        urlBuilder.append("corpid="+WeiXinProperties.getCorpId());
        urlBuilder.append("&corpsecret="+secret);
        String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
        Map<String, String> map = objectMapper.readValue(result, Map.class);
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getKey().equals("errcode")){
                Integer integer = Integer.valueOf(String.valueOf(map.get("errcode")));
                if (integer==0){
                    return  map.get("access_token");

                }
            }
        }
        return null;
    }

}
